package com.jingbit.ai.utils;

import static android.content.Context.ALARM_SERVICE;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.jingbit.ai.alarm.AlarmReceiver;

import java.util.Calendar;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AlarmUtil {
    private static AlarmManager alarmManager;
    private static PendingIntent pendingIntent;

    public static void init(Context ctx) {
        alarmManager = (AlarmManager) ctx.getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(ctx, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent, 0);
    }


    private static final String[] RELATIVE_DAY_WORDS = {"今天", "明天", "后天"};
    private static final String[] RELATIVE_TIME_WORDS = {"早上", "下午", "晚上"};
    private static final Pattern TIME_PATTERN = Pattern.compile(
            "(今天|明天|后天)(早上|下午|晚上|)([0-12]*)点");

    public static int[] parseTime(String input) {
        Matcher matcher = TIME_PATTERN.matcher(input);
        if (matcher.matches()) {
            String dayWord = matcher.group(1);
            String timeWord = matcher.group(2);
            String hourWord = matcher.group(3);

            int dayOffset = getDayOffset(dayWord);
            int timeOffset = getTimeOffset(timeWord, hourWord);
            return new int[]{dayOffset, timeOffset, 0};
        } else {
            return null;
        }
    }

    private static int getDayOffset(String dayWord) {
        for (int i = 0; i < RELATIVE_DAY_WORDS.length; i++) {
            if (dayWord.equals(RELATIVE_DAY_WORDS[i])) {
                return i;
            }
        }
        return 0;
    }

    private static int getTimeOffset(String timeWord, String hourWord) {
        int hourOffset = hourWord.isEmpty() ? 0 : Integer.parseInt(hourWord);
        switch (timeWord) {
            case "早上":
                return hourOffset != 0 ? hourOffset : 8;
            case "下午":
                return 12 + hourOffset;
            case "晚上":
                return hourOffset != 0 ? 12 + hourOffset : 18;
            default:
                try {
                    return Integer.parseInt(timeWord);
                } catch (Exception e) {
                }
        }
        return 0;
    }

    /**
     * 开启闹钟
     *
     * @param context 父类
     * @param day     日
     * @param hour    小时
     * @param minute  分钟
     */
    public static void startAlarm(Context context, int day, int hour, int minute) {
        try {
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(System.currentTimeMillis());//获取当前时间
            //获取当前毫秒值
            long systemTime = System.currentTimeMillis();
            c.setTimeZone(TimeZone.getTimeZone("GMT+8"));//设置时区
            c.set(Calendar.DAY_OF_MONTH, day);//设置几号提醒
            c.set(Calendar.HOUR_OF_DAY, hour);//设置几点提醒
            c.set(Calendar.MINUTE, minute);//设置几分提醒
            //获取上面设置的时间
            long selectTime = c.getTimeInMillis();
            // 如果当前时间大于设置的时间，那么就从第二天的设定时间开始
            if (systemTime > selectTime) {
                c.add(Calendar.DAY_OF_MONTH, 1);
            }
            /* 闹钟时间到了的一个提醒类 */
            Intent intent = new Intent();
            PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
            //得到AlarmManager实例
            AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
            //重复提醒
            am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), ((day * 24* 60 * 60)+(hour * 60 * 60)) * 1000, pi);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭提醒
     *
     * @param context 父类
     */
    private void stopRemind(Context context) {
        try {
            Intent intent = new Intent();
            PendingIntent pi = PendingIntent.getActivity(context, 0,
                    intent, 0);
            AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
            //取消提醒
            am.cancel(pi);
            TTSUtil.doSpeak("已经关闭了闹钟！");
            Intent intents = new Intent("android.intent.action.BOOKCASE_RESTART");
            PendingIntent pis = PendingIntent.getBroadcast(context, 0,
                    intents, 0);
            AlarmManager ams = (AlarmManager) context.getSystemService(ALARM_SERVICE);
            //取消提醒
            ams.cancel(pis);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
